<?php
class category
{
	var $db;
	var $table;
	var $category;
	
	function __construct()
	{
		global $db, $CATEGORY;
		$this->db = &$db;
		$this->category = $CATEGORY;
		$this->table = $gDb['prefix']."category11";		
	}
	
	function category()
	{
		$this->__construct();
	}
	
	function get($catid) 
	{
		$data = $this->db->get_one("SELECT * FROM `$this->table` WHERE `id`='$catid'");
		if(!$data)return false;
		return $data;
	}
	
	function add($category) 
	{
		if(!is_array($category))return false;
		$this->db->insert($this->table,$category);
		$catid = $this->db->insert_id();
		if( $category['parentid'] )
		{
			$category['arrparentid'] = $this->category[$category['parentid']]['arrparentid'].','.$category['parentid'];
			$parentids = explode(',', $category['arrparentid']);
			foreach($parentids as $parentid)
			{
				if($parentid)
				{
					$arrchildid = $this->category[$parentid]['arrchildid'].','.$catid;
					$this->db->query("UPDATE `$this->table` SET child=1,arrchildid='$arrchildid' WHERE catid='$parentid'");
				}
			}			
		} else {
			$category['parentid'] = '0';
		}
		$arrparentid = $category['arrparentid'];
		$this->db->query("UPDATE `$this->table` SET `arrchildid`='$catid',`listorder`=$catid,`arrparentid`='$arrparentid',`parentdir`='$parentdir' WHERE catid=$catid");
		return $catid;
	}
	
	function edit($catid, $category, $setting = array())
	{
		$parentid = $category['parentid'];
		$oldparentid = $this->category[$catid]['parentid'];
		if($parentid != $oldparentid)
		{
			$this->move($catid, $parentid, $oldparentid);
		}

		$category['module'] = $this->module;
		$this->db->update($this->table, $category, "id=$catid");
		return true;
	}
}